﻿namespace Vibstudio.NetConfigurator.UI.Util
{
    using System;
    using System.ComponentModel;
    using System.Reflection;

    public enum OSPlatformVersion
    {
        [Description("Unknown")]
        Unknown,
        [Description("Windows NT 3.51")]
        WindowsNT351,
        [Description("Windows NT 4.0")]
        WindowsNT4,
        [Description("Windows 2000")]
        Windows2000,
        [Description("Windows XP")]
        WindowsXP,
        [Description("Windows 2003 Server")]
        WindowsXP2K3,
        [Description("Windows Vista")]
        WindowsVista,
        [Description("Windows 7")]
        Windows7,
        [Description("Windows 8")]
        Windows8
    }

    public static class OS
    {
        /*---------------------------------------
        OS                         Version number
        -----------------------------------------
        Windows 8                  6.2
        Windows Server 2012        6.2
        Windows 7 Service Pack 1   6.1
        Windows 7                  6.1
        Windows Server 2008 R2     6.1
        Windows Server 2008        6.0
        Windows Vista              6.0
        Windows Server 2003 R2     5.2
        Windows Server 2003        5.2
        Windows XP 64-Bit Edition  5.2
        Windows XP 	               5.1
        Windows 2000               5.0
        ---------------------------------------*/

        // Major NT Kernel Versions
        private const int WINDOWS_NT4_KERNEL = 4;
        private const int WINDOWS_NT5_KERNEL = 5;

        // Minor NT Kernel Versions
        private const int WINDOWS_2000 = 0;
        private const int WINDOWS_XP = 1;
        private const int WINDOWS_XP2003 = 2;

        // Major NT Kernel Versions
        private const int WINDOWS_NT6_KERNEL = 6;

        // Minor NT Kernel Versions
        private const int WINDOWS_VISTA = 0;
        private const int WINDOWS_7 = 1;
        private const int WINDOWS_8 = 2;

        public static OSPlatformVersion GetPlatformVersion()
        {
            OperatingSystem os = Environment.OSVersion;

            var platformVersion = OSPlatformVersion.Unknown;
            switch (os.Version.Major)
            {
                // Windows NT 4.0
                case WINDOWS_NT4_KERNEL:
                    platformVersion = OSPlatformVersion.WindowsNT4;
                    break;
                case WINDOWS_NT5_KERNEL:
                    switch (os.Version.Minor)
                    {
                        // Windows 2000
                        case WINDOWS_2000:
                            platformVersion = OSPlatformVersion.Windows2000;
                            break;
                        // Windows Xp
                        case WINDOWS_XP:
                            platformVersion = OSPlatformVersion.WindowsXP;
                            break;
                        // Windows Xp 2003 Server
                        case WINDOWS_XP2003:
                            platformVersion = OSPlatformVersion.WindowsXP2K3;
                            break;
                    }
                    break;
                // Windows Vista and Server 2008
                case WINDOWS_NT6_KERNEL:
                    switch (os.Version.Minor)
                    {
                        case WINDOWS_VISTA:
                            platformVersion = OSPlatformVersion.WindowsVista;
                            break;
                        case WINDOWS_7:
                            platformVersion = OSPlatformVersion.Windows7;
                            break;
                        case WINDOWS_8:
                            platformVersion = OSPlatformVersion.Windows8;
                            break;
                    }
                    break;
            }

            return platformVersion;
        }

        public static string GetPlatformVersionDescription(OSPlatformVersion platformVersion)
        {
            string description = String.Empty;
            Type t = platformVersion.GetType();
            MemberInfo[] members = t.GetMember(platformVersion.ToString());
            
            if (members.Length == 1)
            {
                object[] attrs = members[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                
                if (attrs.Length == 1)
                {
                    description = ((DescriptionAttribute)attrs[0]).Description;
                }
            }

            return description;
        }

        public static string GetVersionDetails()
        {
            OperatingSystem os = Environment.OSVersion;
            return string.Format("v{0}.{1}.{2}  {3}", os.Version.Major, os.Version.Minor, os.Version.Build, os.ServicePack);
        }

    }
}
